<?php
/**
 * Created by PhpStorm.
 * User: zu
 * Date: 14-10-9
 * Time: 上午9:49
 */

/**
 * Class Accountsystemserverapi
 * 进销存系统提供给供应商应付账款系统的API类
 */
class Accountsystemapiserver
{
    public static function executeSql($sql, $cols = NULL, $db = 'newerp', $type){
        if($type != 'UPDATE'){
            if($cols && is_array($cols))
                return json_encode(DB::query(Database::SELECT, $sql)->execute($db)->as_array($cols[0], $cols[1]));

            if(empty($db))
                return json_encode(DB::query(Database::SELECT, $sql)->execute()->as_array());
            else
                return json_encode(DB::query(Database::SELECT, $sql)->execute($db)->as_array());
        }else{
            return DB::query(Database::UPDATE, $sql)->execute($db);
        }

    }

    /**
     * @author locove
     * @return array
     * @note　获取指定数据库数据表的所有ID
     */
    public static function putNewerpIdByName()
    {
        $ids_arr=array();
        foreach(func_get_args() as $conditions){

            foreach($conditions as $db=>$tables){
                if($tables && is_array($tables)){
                    foreach($tables as $table=>$condition){
                        $condition['=']['offset']=$conditions['offset'];
                        $condition['=']['page_size']=$conditions['page_size'];
                        $re=self::condition(DB::select('id')->from($table),$condition);
                        $ids_arr[$table]=$re->execute($db)->as_array(NULL,'id');
                        if($table=='purchaseorders'&&$ids_arr[$table]){
                            $table_detail='purchaseorder_details';
                            $ids_arr[$table_detail]=DB::select('id')->from($table_detail)
                                ->where('purchaseorder_id','IN',$ids_arr[$table])
                                ->execute($db)
                                ->as_array(NULL,'id');
                        }
                    }
                }
            }
        }
        //return $re->compile('newerp');
        return $ids_arr;
    }

    /**
     * @author locove
     * @return array
     * @note　根据指定数据库数据表的ID获取对应数据
     */
    public static function putDataById()
    {
        $datas=array();
        $accounts=array();
        $paras=func_get_args();
        foreach($paras[1] as $db=>$tbs){
            foreach($tbs as $tb=>$columns){
                $datas[$tb]=DB::select()->from($tb)
                    ->select_array($columns)
                    ->where('id','IN',$paras[0])
                    ->execute($db)
                    ->as_array();
                if($tb=='purchaseorders'){
                    $accounts=DB::select('poid','payaccountinfo_id')
                        ->from('purchasesupplier')
                        ->where('poid','IN',$paras[0])
                        ->execute($db)
                        ->as_array('poid','payaccountinfo_id');
                    foreach($datas[$tb] as $key=>$data){
                        $datas[$tb][$key]['ac_id']=$accounts[$data['pu_id']];
                    }
                }
            }
            /*foreach($tbs as $tb=>$columns){
                if($paras[0][$tb])
                $datas[$tb]=DB::select()->from($tb)
                    ->select_array($columns)
                    ->where('id','IN',$paras[0][$tb])
                    ->execute($db)
                    ->as_array();
                else $datas[$tb]=array();
            }*/
        }
        //return $accounts;
        return $datas;
        /*$new_name='pu_id';
        if($paras[2]=='purchaseorder_details')$new_name='pud_id';
        if(isset($paras[1])&&is_array($paras[1]))
            $columns=$paras[1];
        else
            $columns=array('id',$new_name);
        return DB::select()->from($paras[2])
            ->select_array($columns)
            ->where('id','IN',$paras[0])
            ->execute('newerp')
            ->as_array();*/
    }

    /**
     * @author locove
     * @param $re
     * @param $condition
     * @return obj
     * @note　将传递过来的条件进行拼凑
     */
    public static function condition($re,$condition){
        foreach($condition as $logic=>$val_arr){
            if($val_arr&&is_array($val_arr)){
                foreach($val_arr as $cl=>$val){
                    if($cl=='page_size')
                        $re->limit($val);
                    elseif($cl=='offset')
                        $re->offset($val);
                    else
                        $re->where($cl,$logic,$val);
                }
            }
        }
        return $re;
    }

    /**
     * 获取用户信息USER表
     * $feilds
     * $condtion
     * $as_array
     * @author zhjx
     * @return array
     */
    public static function getUsersBy()
    {
        $params = func_get_args();

        $re = DB::select('id')->from('users');;

        /**
         * 字段信息
         */
        if(isset($params[0])
            &&$params[0]
            &&is_array($params[0]))
        {
           $re->select_array($params[0]);
        }

        /**
         * 条件信息
         */
        if(isset($params[1])
            &&$params[1]
            &&is_array($params[1]))
        {
           if(isset($params[1]['id']['eq'])
           && $params[1]['id']['eq'])
           {
              $re->where('id','=',$params[1]['id']['eq']);
           }

           if(isset($params[1]['id']['in'])
           && $params[1]['id']['in']
           && is_array($params[1]['id']['in']))
           {
               $re->where('id','IN',$params[1]['id']['in']);
           }

           if(isset($params[1]['id']['not in'])
           && $params[1]['id']['not in']
           && is_array($params[1]['id']['not in']))
           {
               $re->where('id','NOT IN',$params[1]['id']['not in']);
           }
        }

        if(isset($params[2])
        && $params[2]
        && is_array($params[2]))
        {
           $params[2] = array_slice($params[2],0,2);
        }
        $params[2][0] = isset($params[2][0])?$params[2][0]:NULL;
        $params[2][1] = isset($params[2][1])?$params[2][1]:NULL;

        echo $re->compile();

        return $re->execute()->as_array($params[2][0],$params[2][1]);

    }


    /**
     * @param array $feilds
     * @param array $condtion
     * @param array $as_array
     */
    public static function getPaywaysBy(array $feilds=NULL,array $condtion=NULL,array $as_array=NULL)
    {
        $re = DB::select('id')->from('payways');
        if($feilds)
        {
            $re->select_array($feilds);
        }

        if(isset($condtion['id']['eq']))
        {
           $re->where('id','=',$condtion['id']['eq']);
        }

        if(isset($condtion['id']['in']))
        {
            $re->where('id','IN',$condtion['id']['eq']);
        }

        if(isset($condtion['id']['not in']))
        {
            $re->where('id','NOT IN',$condtion['id']['eq']);
        }

        if(isset($condtion['paywayname']['eq']))
        {
            $re->where('paywayname','=',$condtion['paywayname']['eq']);
        }

        if(isset($condtion['status']['eq']))
        {
            $re->where('status','=',$condtion['status']['eq']);
        }

        if(isset($condtion['status']['in']))
        {
            $re->where('status','IN',$condtion['status']['in']);
        }

        $as_array = array_slice($as_array,0,2);
        $as_array[0] = isset($as_array[0])?$as_array[0]:NULL;
        $as_array[1] = isset($as_array[1])?$as_array[1]:NULL;

        echo $re->compile('newerp');
        return $re->execute('newerp')->as_array($as_array[0],$as_array[1]);
    }

    public static function getPayaccounts(array $feilds=NULL,array $condtion=NULL,array $as_array=NULL)
    {
        $re = DB::select(array('supplierpayaccount.supplier_id','su_id'))->from('supplierpayaccount');
        $re->join('payaccountinfo')->on('supplierpayaccount.payaccountinfo_id','=','payaccountinfo.id')
            ->where('supplierpayaccount.active_status','=',1);

        if($feilds)
        {
            $re->select_array($feilds);
        }

        /**
         * 供应商ID
         */
        if(isset($condtion['su_id']['in']))
        {
            $re->where('supplierpayaccount.supplier_id','IN',$condtion['su_id']['in']);
        }

        /**
         * 付款信息ID
         */
        if(isset($condtion['id']['in']))
        {
           $re->where('payaccountinfo.id','in',$condtion['id']['in']);
        }


        if(isset($condtion['limit']))
        {
            $re->limit($condtion['limit']);
        }

        if(isset($condtion['offset']))
        {
           $re->offset($condtion['offset']);
        }

        $as_array = array_slice($as_array,0,2);
        $as_array[0] = isset($as_array[0])?$as_array[0]:NULL;
        $as_array[1] = isset($as_array[1])?$as_array[1]:NULL;

        echo $re->compile('newerp');
        return $re->execute('newerp')->as_array($as_array[0],$as_array[1]);
    }

    /**
     * 回写付款记录
     * @author zhjx
     * @param null $datas
     * @return object
     */
    public static function doPoPay($datas = NULL)
    {
        if(!empty($datas)){
            return DB::insert('purchaseorder_pays')
                ->columns(array_keys($datas))
                ->values($datas)
                ->execute('newerp');
        }
        return FALSE;

       /*if($datas)
       {
           $feilds = array();
           $values = array();

           $feilds[] = 'purchaseorder_id';
           $feilds[] = 'supplier_id';
           $feilds[] = 'pay_way';
           $feilds[] = 'pay_date';
           $feilds[] = 'payer';
           $feilds[] = 'amount_paid';
           $feilds[] = 'remark';

           foreach($feilds as $feild)
           {
               $values[] = isset($datas[$feild])?$datas[$feild]:'';
           }

           if($values)
           {
               $re = DB::insert('purchaseorder_pays')
                   ->columns($feilds)
                   ->values($values);

               echo $re->compile('newerp');

               return $re->execute('newerp');
           }
       }*/
    }

    /**
     * 获取入库单数据
     * @param string $sql
     * @return mixed
     * @author hm
     */
    public static function searchWaOrders($sql=''){
        return DB::query(Database::SELECT,$sql)->execute('newerp')->as_array();
    }

    /**
     * 获取入库单数据1
     * @param string $sql
     * @return mixed
     * @author hm
     */
    public static function searchWaOrders1($sql=''){
        return DB::query(Database::SELECT,$sql)->execute('newerp')->as_array();
    }

    /**
     * 获取入库单数据2
     * @param string $sql
     * @return mixed
     * @author hm
     */
    public static function searchWaOrders2($sql=''){
        return DB::query(Database::SELECT,$sql)->execute('newerp')->as_array();
    }

    /**
     * 获取入库单数据3
     * @param string $sql
     * @return mixed
     * @author hm
     */
    public static function searchWaOrders3($sql=''){
        return DB::query(Database::SELECT,$sql)->execute('newerp')->as_array();
    }

    /**
     * 获取入库单数据old
     * @param array $fs
     * @param array $condition
     * @return mixed
     * @author hm
     */
    public static function searchWaOrdersOld(array $fs = null,array $condition = null)
    {
        $re = DB::select('id')->from('warehouseorders');
        if($fs)
        {
            $re->select_array($fs);
        }
        //入库单号
        if(isset($condition['id']['eq']))
        {
            $re->where('warehouseorders.id','=',$condition['id']['eq']);
        }
        //采购单号
        if(isset($condition['purchaseorders_id']['eq']))
        {
            $re->where('warehouseorders.purchaseorders_id','=',$condition['purchaseorders_id']['eq']);
        }
        //供应商id
        if(isset($condition['supplier_id']['eq']))
        {
            $re->where('warehouseorders.supplier_id','=',$condition['supplier_id']['eq']);
        }
        if(isset($condition['limit']))
        {
            $re->limit($condition['limit']);
        }
        if(isset($condition['offset']))
        {
            $re->offset($condition['offset']);
        }
        return $re->execute('newerp')->as_array();
    }

}